package cn.iocoder.yudao.module.signature.validatap12status.cermb;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 苹果证书验证器使用示例
 */
public class Example {
    
    private static final Logger logger = LoggerFactory.getLogger(Example.class);
    
    public static void main(String[] args) {
        // 示例用法
        validateCertificateExample();
    }
    
    /**
     * 验证证书示例
     */
    public static void validateCertificateExample() {
        // 创建验证器实例
        AppleCertificateValidator validator = new AppleCertificateValidator();
        
        // 设置文件路径和密码
        String p12FilePath = "D:\\Users\\liujq\\wechat\\WeChat Files\\wxid_4qjp6xqz5cpw22\\FileStorage\\File\\2025-08\\P7YOSI_证书文件(密码1).p12";
        String p12Password = "1";
        String provisioningProfilePath = "D:\\Users\\liujq\\wechat\\WeChat Files\\wxid_4qjp6xqz5cpw22\\FileStorage\\File\\2025-08\\65O9RQ_描述文件.mobileprovision";
        
        try {
            // 执行验证
            AppleCertificateValidator.ValidationResult result = 
                validator.validateCertificateAndProfile(p12FilePath, p12Password, provisioningProfilePath);
            
            // 输出验证结果
            System.out.println("验证结果: " + (result.isValid() ? "通过" : "失败"));
            System.out.println("消息: " + result.getMessage());
            
            if (result.getDetails() != null && !result.getDetails().isEmpty()) {
                System.out.println("详细信息:");
                for (java.util.Map.Entry<String, String> entry : result.getDetails().entrySet()) {
                    System.out.println("  " + entry.getKey() + ": " + entry.getValue());
                }
            }
            
        } catch (Exception e) {
            logger.error("验证过程中发生异常", e);
            System.err.println("验证失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量验证示例
     */
    public static void batchValidateExample() {
        AppleCertificateValidator validator = new AppleCertificateValidator();
        
        // 定义多个证书和描述文件对
        String[][] certificatePairs = {
            {"cert1.p12", "password1", "profile1.mobileprovision"},
            {"cert2.p12", "password2", "profile2.mobileprovision"},
            {"cert3.p12", "password3", "profile3.mobileprovision"}
        };
        
        for (String[] pair : certificatePairs) {
            String p12File = pair[0];
            String password = pair[1];
            String profileFile = pair[2];
            
            System.out.println("正在验证: " + p12File + " 和 " + profileFile);
            
            AppleCertificateValidator.ValidationResult result = 
                validator.validateCertificateAndProfile(p12File, password, profileFile);
            
            System.out.println("结果: " + (result.isValid() ? "✓ 通过" : "✗ 失败"));
            System.out.println("原因: " + result.getMessage());
            System.out.println("---");
        }
    }
    
    /**
     * 仅验证证书信息示例
     */
    public static void validateCertificateOnlyExample() {
        String p12FilePath = "path/to/your/certificate.p12";
        String p12Password = "your_p12_password";
        
        try {
            // 加载证书
            java.security.KeyStore keyStore = CertificateUtils.loadP12Certificate(p12FilePath, p12Password);
            if (keyStore == null) {
                System.err.println("无法加载证书文件");
                return;
            }
            
            // 提取证书信息
            CertificateInfo certInfo = CertificateUtils.extractCertificateInfo(keyStore);
            if (certInfo == null) {
                System.err.println("无法提取证书信息");
                return;
            }
            
            // 输出证书信息
            System.out.println("证书信息:");
            System.out.println("  别名: " + certInfo.getAlias());
            System.out.println("  主题: " + certInfo.getSubjectDN());
            System.out.println("  颁发者: " + certInfo.getIssuerDN());
            System.out.println("  序列号: " + certInfo.getSerialNumber());
            System.out.println("  指纹: " + certInfo.getFingerprint());
            System.out.println("  类型: " + certInfo.getCertificateType());
            System.out.println("  有效期: " + certInfo.getNotBefore() + " 至 " + certInfo.getNotAfter());
            
            // 检查证书是否有效
            boolean isValid = CertificateUtils.isCertificateValid(certInfo);
            System.out.println("证书是否有效: " + (isValid ? "是" : "否"));
            
        } catch (Exception e) {
            logger.error("验证证书时发生异常", e);
            System.err.println("验证失败: " + e.getMessage());
        }
    }
    
    /**
     * 仅验证描述文件示例
     */
    public static void validateProfileOnlyExample() {
        String profilePath = "path/to/your/profile.mobileprovision";
        
        try {
            // 解析描述文件
            ProvisioningProfileInfo profileInfo = ProvisioningProfileUtils.parseProvisioningProfile(profilePath);
            if (profileInfo == null) {
                System.err.println("无法解析描述文件");
                return;
            }
            
            // 输出描述文件信息
            System.out.println("描述文件信息:");
            System.out.println("  UUID: " + profileInfo.getUuid());
            System.out.println("  名称: " + profileInfo.getName());
            System.out.println("  App ID名称: " + profileInfo.getAppIdName());
            System.out.println("  Team ID: " + profileInfo.getTeamIdentifier());
            
            if (profileInfo.getCertificateFingerprints() != null) {
                System.out.println("  证书指纹数量: " + profileInfo.getCertificateFingerprints().length);
                for (String fingerprint : profileInfo.getCertificateFingerprints()) {
                    System.out.println("    " + fingerprint);
                }
            }
            
            if (profileInfo.getDeviceIds() != null) {
                System.out.println("  设备数量: " + profileInfo.getDeviceIds().length);
            }
            
        } catch (Exception e) {
            logger.error("验证描述文件时发生异常", e);
            System.err.println("验证失败: " + e.getMessage());
        }
    }
} 